package cn.nuomi.shop.api.dao.impl;

import cn.nuomi.shop.api.dao.UserDao;
import cn.nuomi.shop.api.entity.Goods;
import cn.nuomi.shop.api.entity.User;
import cn.nuomi.shop.api.util.DBUtil;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.math.BigDecimal;
import java.sql.*;
import java.util.List;

import static cn.nuomi.shop.api.constants.MysqlInfo.JDBC_PASSWORD;
import static cn.nuomi.shop.api.constants.MysqlInfo.JDBC_USER_NAME;

/**
 * @date: 2021/4/26 14:55
 * @description:
 */
public class UserDaoImpl implements UserDao {

    private QueryRunner queryRunner = new QueryRunner(DBUtil.getDataSource());


    @Override
    public User selectByUserName(String paramsUserName) {
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/nuomi?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
            Connection connection = DriverManager.getConnection(url,JDBC_USER_NAME,JDBC_PASSWORD);
            Statement statement = connection.createStatement();
            String sql = "SELECT * FROM `user` WHERE user_name='%s'";
            sql = String.format(sql, paramsUserName);
            System.out.println(sql);
            ResultSet resultSet = statement.executeQuery(sql);
            User dbUser = null;
            while (resultSet.next()) {
                dbUser = new User();
                int id = resultSet.getInt("id");
                dbUser.setId(id);
                String userName = resultSet.getString("user_name");
                dbUser.setUserName(userName);
                dbUser.setPassword(resultSet.getString("password"));
                dbUser.setUserBalance(resultSet.getBigDecimal("user_balance"));
            }
            resultSet.close();
            statement.close();
            connection.close();
            return dbUser;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public User registerByUserName(String userName, String password) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/nuomi?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
            Connection connection = DriverManager.getConnection(url,JDBC_USER_NAME,JDBC_PASSWORD);
            Statement statement = connection.createStatement();
            String sql = "INSERT INTO `user`( `user_name`, `password`) VALUES ('%s','%s')";
            sql = String.format(sql, userName,password);
            System.out.println(sql);
            int resultSet = statement.executeUpdate(sql);
            System.out.println(resultSet);
            statement.close();
            connection.close();

            User dbUser = selectByUserName(userName);
            System.out.println(dbUser);
            return dbUser;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int changeBalanceByUserName(String userName, BigDecimal balance) {

        Connection connection = null;
        PreparedStatement statement = null;

        try{
            connection = DBUtil.getConnection();

            String sql = "UPDATE user SET user_balance = ? WHERE user_name=?";

            //获取发送执行sql对象工具
            statement = connection.prepareStatement(sql);
            statement.setBigDecimal(1, balance);
            statement.setString(2, userName);

            int result = statement.executeUpdate();
            return result;

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeAll(null,statement,null);
        }

        return 0;
    }
}
